import request from "@/utils/request";

const AUTH_BASE_URL = "/api/v1/auth";

const AuthAPI = {
  login(data: LoginFormData) {
    data.type = 1;
    return request<any, LoginResult>({
      url: `${AUTH_BASE_URL}/login`,
      method: "post",
      data,
    });
  },
  /** 短信登录接口*/
  smsLogin(data: { mobile: string; smsCode: string; type: number }) {
    return request<any, LoginResult>({
      url: `${AUTH_BASE_URL}/login`, // 短信登录接口
      method: "post",
      data,
    });
  },
  /** 获取用户身份接口*/
  getUserIdentity(data: { username: string; password: string }) {
    return request<any, LoginResult>({
      url: `${AUTH_BASE_URL}/getUserIdentity`,
      method: "post",
      params: data,
    });
  },
  /** 设置用户身份接口*/
  setUserIdentity(data: { companyId: string; virtualId: string; merchantUserId: string }) {
    return request<any, LoginResult>({
      url: `${AUTH_BASE_URL}/setUserIdentity`,
      method: "post",
      params: data,
    });
  },
  /** 刷新 token 接口*/
  refreshToken(refreshToken: string) {
    return request<any, LoginResult>({
      url: `${AUTH_BASE_URL}/refresh-token`,
      method: "post",
      params: { refreshToken: refreshToken },
      headers: {
        Authorization: "no-auth",
      },
    });
  },

  /** 注销登录接口 */
  logout() {
    return request({
      url: `${AUTH_BASE_URL}/logout`,
      method: "post",
    });
  },

  /** 获取验证码接口*/
  getCaptcha() {
    return request<any, CaptchaInfo>({
      url: `${AUTH_BASE_URL}/captcha`,
      method: "get",
    });
  },
};

export default AuthAPI;

/** 登录表单数据 */
export interface LoginFormData {
  /** 用户名 */
  username: string;
  /** 密码 */
  password: string;
  /** 验证码缓存key */
  captchaKey: string;
  /** 验证码 */
  captchaCode: string;
  /**类型 */
  type?: number;
}

/** 短信登录表单数据 */
export interface SmsLoginFormData {
  /** 手机号 */
  mobile: string;
  /** 验证码 */
  smsCode: string;
  /**类型 */
  type?: number;
}
/** 登录响应 */
export interface LoginResult {
  /** 访问令牌 */
  accessToken: string;
  /** 刷新令牌 */
  refreshToken: string;
  /** 令牌类型 */
  tokenType: string;
  /** 过期时间(秒) */
  expiresIn: number;
}

/** 验证码信息 */
export interface CaptchaInfo {
  /** 验证码缓存key */
  captchaKey: string;
  /** 验证码图片Base64字符串 */
  captchaBase64: string;
}
